package com.hopu.controller.exportc;

import com.hopu.domain.ContractC;
import com.hopu.domain.export.ExportC;
import com.hopu.service.ContractCService;
import com.hopu.service.ExportCService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/export")
/**
 * 出口报运模块
 */
public class ExportCController {

    @Autowired
    ContractCService contractCService;
    @Autowired
    ExportCService exportCService;
    @RequestMapping("/alllist")
    /**
     * 查询所有启用的合同
     */
    public String alllist(Model model){
        Integer i = 1;
        List<ContractC> contractCS = contractCService.selectBystate(i);
        model.addAttribute("dataList",contractCS);
        return "cargo/export/jContractList";
    }

    /**
     * 去查询页面
     * @return
     */
    @RequestMapping("/toinsert")
    public String toinsert(@RequestParam("contractId") String[] contractIds){

        exportCService.insert(contractIds);
        return "redirect:findExportC";
    }

    /**
     * 查詢頁面
     * @return
     */
    @RequestMapping("/findExportC")
    public String findExportC(Model model){
        List<ExportC> list = exportCService.selectByExample();
        model.addAttribute("dataList",list);
        return "cargo/export/jExportList";
    }

    /**
     * 上報
     * @param exportId
     * @param map
     * @return
     */
    @RequestMapping("/state")
    public String state(String[] exportId, Map<String, Object> map){
        if (exportId != null) {
            for (String fid : exportId) {
                map.put("exportId",fid);
                Integer i = 1;
                map.put("state",i);
                exportCService.state(map);
            }
        }
        return "redirect:findExportC";
    }

    /**
     * 取消
     * @param exportId
     * @param map
     * @return
     */
    @RequestMapping("/cancel")
    public String cancel(String[] exportId, Map<String, Object> map){
        if (exportId != null) {
            for (String fid : exportId) {
                map.put("exportId",fid);
                Integer i = 0;
                map.put("state",i);
                exportCService.state(map);
            }
        }
        return "redirect:findExportC";
    }

    /**
     * 去修改
     * @param exportId
     * @param model
     * @return
     */
    @RequestMapping("/toupdate")
    public String toUpdate(@RequestParam(value = "exportId",required = false) String[] exportId, Model model){
        if (exportId.length==1){
            String s = exportId[0];
            ExportC exportC = exportCService.selectByPrimaryKey(s);
            model.addAttribute("obj",exportC);
        }
        //准备批量修改控件的数据mrecord
        String mrecordData = exportCService.getMrecordData(exportId);
        model.addAttribute("mRecordData",mrecordData);
        return "cargo/export/jExportUpdate";
    }

    @RequestMapping("updates")
    public String updates(ExportC exportC,
                          String[]  del,
                         Integer[] mr_orderNo,
                         Integer[] mr_cnumber,
                         Double[] mr_grossWeight,
                         Double[] mr_netWeight,
                         Double[] mr_sizeLength,
                         Double[] mr_sizeWidth,
                         Double[] mr_sizeHeight,
                         Double[] mr_exPrice,
                         Double[] mr_tax,
                         Integer[] mr_changed){
        for (int a =0 ;a<mr_changed.length; a++){
            System.out.println(mr_changed[a]);
        }
        exportCService.update(exportC,del,mr_orderNo,mr_cnumber,mr_grossWeight,mr_netWeight,mr_sizeLength,mr_sizeWidth,
                            mr_sizeHeight,mr_exPrice,mr_tax);
        return "redirect:findExportC";
    }
    @RequestMapping("/delete")
    public String delete(String[] exportId){
        for (String s : exportId) {
            exportCService.deleteByPrimaryKey(s);
        }
        return "redirect:findExportC";
    }
}
